package com.cloudcentral.enums;

import com.cloudcentral.util.SpringContextUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

/**
 * Create by Administrator
 * 2023/12/7 10:39
 */
@Getter
@Schema(description = "台站状态")
public enum RadioStatus {
  OFFLINE("离线"),
  IDLE("空闲"),
  SENDING("发送"),
  RECEIVING("接收");

  @Schema(description = "名称")
  private final String name;

  RadioStatus(String name) {
    this.name = name;
  }

  public static void value(Integer radioId, RadioStatus mode) {
    RedissonClient redissonClient = SpringContextUtils.getBean(RedissonClient.class);
    RBucket<String> rBucket = redissonClient.getBucket("Radio-Status-" + radioId);
    if (mode.equals(OFFLINE)) rBucket.delete();
    else rBucket.set(mode.name());
  }

  public static RadioStatus value(Integer radioId) {
    RedissonClient redissonClient = SpringContextUtils.getBean(RedissonClient.class);
    RBucket<String> rBucket = redissonClient.getBucket("Radio-Status-" + radioId);
    if (rBucket == null || rBucket.get() == null) return OFFLINE;
    else return valueOf(rBucket.get());
  }
}
